package com.unbank.net;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import com.google.gson.Gson;
import com.unbank.ES.ESClient;
import com.unbank.entity.CrawlDataWithHeat;
import com.unbank.entity.FetchData;
import com.unbank.entity.News;

public class Test_Fetcher {

	private final static  int PAGESIZE=20;
	
	
	private static String host="http://10.0.2.147:8080/recommend/getDocByLabelFilter.htm";
	
	@Test
	public void test() throws ClientProtocolException, IOException {
		Fetcher fetcher = new Fetcher();
		
		/**
		 * 根据Tag搜索：http://10.0.2.25:8080/recommend/getDocByLabels.htm?tag=银行&begin=0&num=20
		 * 根据Tag和关键词搜索：http://10.0.2.25:8080/recommend/getDocByLabels.htm?tag=%E9%93%B6%E8%A1%8C&keyWords=%E8%B4%B7%E6%AC%BE/%E9%A3%8E%E9%99%A9&begin=0&num=20
		 * 根据Tag、关键词、websiteID搜索：http://localhost:8080/recommend/getDocByLabelFilter.htm?tag=%E7%BB%8F%E6%B5%8E&keyWords=%E6%B0%B4%E5%88%A9%E5%8E%85/%E7%A6%BB%E5%B2%B8&begin=0&num=72&webSiteId=15
		 * 
		 */
		
		Map<String,String> map=new HashMap<String,String>();
		map.put("tag", "财经");
		//map.put("keyWords", "存款");
		//map.put("webSiteId", "502");
		map.put("begin", "0");
		map.put("num", "20");
		
		Long time1=System.currentTimeMillis();
		
		//String str = fetcher.fetch(map,host);
		String str = fetcher.fetch(map);
		
		Long time2=System.currentTimeMillis();
		
		System.out.println("从图数据库获取DocID耗时："+(time2-time1));
		
		Gson gson = new Gson();
		System.out.println(str);
		FetchData fd = gson.fromJson(str, FetchData.class);
		System.out.println(fd.getData().size());
		List<Integer> list=new ArrayList<Integer>();
		
		for(CrawlDataWithHeat cdwh:fd.getData()){
			list.add(cdwh.getDocId());
		}
		
		ESClient es=new ESClient();
		
		Long time3=System.currentTimeMillis();
		List<News> newsList= es.SearchByCrawlIdsOrderByCrawlTime(list, 0, PAGESIZE);
		//List<News> newsList= es.SearchByCrawlIds(list, 0, 20);
		Long time4=System.currentTimeMillis();
		System.out.println("从检索文件中获取文档耗时："+(time4-time3));
		//System.out.println(newsList.size());
		for(News n:newsList){
			//System.out.println("Crawl_ID:"+n.getCrawl_id()+"title:"+n.getTitle()+"  content:"+n.getContent());
			System.out.println("Crawl_ID:"+n.getCrawl_id()+"	title:"+n.getTitle());
			//System.out.println(n);
		}
		/*Long time5=System.currentTimeMillis();
		List<News> newsList2= es.SearchByWord("银行", 0, 20);
		Long time6=System.currentTimeMillis();
		System.out.println(time6-time5);
		for(News n:newsList2){
			System.out.println("Crawl_ID:"+n.getCrawl_id()+"	title:"+n.getTitle());
		}*/
		//System.out.println(newsList);
		//System.out.println(fd);
		
	}

	@Test
	public void test2() throws ClientProtocolException, IOException {
	Fetcher fetcher = new Fetcher();
		
		/**
		 * 根据Tag搜索：http://10.0.2.25:8080/recommend/getDocByLabels.htm?tag=银行&begin=0&num=20
		 * 根据Tag和关键词搜索：http://10.0.2.25:8080/recommend/getDocByLabels.htm?tag=%E9%93%B6%E8%A1%8C&keyWords=%E8%B4%B7%E6%AC%BE/%E9%A3%8E%E9%99%A9&begin=0&num=20
		 * 根据Tag、关键词、websiteID搜索：http://localhost:8080/recommend/getDocByLabelFilter.htm?tag=%E7%BB%8F%E6%B5%8E&keyWords=%E6%B0%B4%E5%88%A9%E5%8E%85/%E7%A6%BB%E5%B2%B8&begin=0&num=72&webSiteId=15
		 */
		
		Map<String,String> map=new HashMap<String,String>();
		map.put("tag", "财经");
		//map.put("keyWords", "存款");
		map.put("webSiteId", "763");
		
		map.put("begin", "0");
		map.put("num", "20");
		
		Long time1=System.currentTimeMillis();
		
		String str = fetcher.fetch(map,host);
		//String str = fetcher.fetch(map);
		System.out.println(str);
		Long time2=System.currentTimeMillis();
		
		System.out.println("从图数据库获取DocID耗时："+(time2-time1));
		
		Gson gson = new Gson();
		FetchData fd = gson.fromJson(str, FetchData.class);
		System.out.println(fd.getData().size());
		List<Integer> list=new ArrayList<Integer>();
		
		for(CrawlDataWithHeat cdwh:fd.getData()){
			list.add(cdwh.getDocId());
		}
		
		ESClient es=new ESClient();
		
		Long time3=System.currentTimeMillis();
		List<News> newsList= es.SearchByCrawlIdsOrderByCrawlTime(list, 0, PAGESIZE);
		//List<News> newsList= es.SearchByCrawlIds(list, 0, 20);
		Long time4=System.currentTimeMillis();
		System.out.println("从检索文件中获取文档耗时："+(time4-time3));
		//System.out.println(newsList.size());
		for(News n:newsList){
			//System.out.println("Crawl_ID:"+n.getCrawl_id()+"title:"+n.getTitle()+"  content:"+n.getContent());
			System.out.println("Crawl_ID:"+n.getCrawl_id()+"	title:"+n.getTitle());
			//System.out.println(n);
		}
	}


	@Test
	public void test_searchByWord(){
		ESClient es=new ESClient();
		Long time5=System.currentTimeMillis();
		List<News> newsList2= es.SearchByWord("银行", 0, 20);
		Long time6=System.currentTimeMillis();
		System.out.println(time6-time5);
		for(News n:newsList2){
			System.out.println("Crawl_ID:"+n.getCrawl_id()+"	title:"+n.getTitle());
		}
	}

	@Test
	public void test_index() throws ClientProtocolException, IOException{
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httpost = new HttpPost("http://localhost/SearchPlatform/rest/index/doIndex2");
		List<NameValuePair> nvps = new ArrayList <NameValuePair>();
		nvps.add(new BasicNameValuePair("crawl_id","1155527"));
		httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		HttpResponse response = httpclient.execute(httpost);
		HttpEntity entity = response.getEntity();
		String s= EntityUtils.toString(entity);
	}
	
	@Test
	public void test_index2() throws ClientProtocolException, IOException{
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httpost = new HttpPost("http://10.0.2.146:8080/SearchPlatform/rest/index/oper");
		List<NameValuePair> nvps = new ArrayList <NameValuePair>();
		nvps.add(new BasicNameValuePair("crawl_id","1142223"));
		nvps.add(new BasicNameValuePair("title","光大银行直销银行将于今年6月上线"));
		nvps.add(new BasicNameValuePair("url","http://zx.cebnet.com.cn/20150318/101149404.html"));
		nvps.add(new BasicNameValuePair("content","<div> <div> <p>据理财周报记者了解，目前光大银行直销银行的筹备已经进入最后阶段。光大银行方面称，将于今年6月正式推出直销银行服务。</p> <p>此前，已有北京银行、民生银行、兴业银行、恒丰银行、华夏银行、平安银行等近20家银行的直销银行纷纷上线。</p> <p>近期，工行的“工银融e行”也已悄然上线。民生银行直销银行刚刚过了周岁生日，其直销银行的资产规模目前已经接近250亿元，客户数量超过了160万。</p> <p>据了解，即将上线的光大银行直销银行将突破传统银行“高大全”的形象，定位“小轻新”，客群定位上有别于以往的“覆盖千家万户”，而是专注服务“忙、潮、精”的年轻人群，产品设计上也不再要求全面，而是主打快速理财、缴费、便民精品等，力求简单、实惠。</p> <p>“光大银行素以‘理财银行’在业内外扬名，为配合直销银行上线，光大银行特地根据目标客群的需求，推出了财富保、定存宝、积存金、实物金和e理财等优质理财产品，并上线“光大云缴费”平台。”光大银行电子银行部总经理杨兵兵表示。</p> <p>据理财周报记者了解，光大银行的直销银行隶属于电子银行部，目前尚未单独设立部门。</p> <p>责任编辑：Rachel</p> </div></div>"));
		nvps.add(new BasicNameValuePair("newsDate",new Date().getTime()+""));
		nvps.add(new BasicNameValuePair("crawlDate",new Date().getTime()+""));
		nvps.add(new BasicNameValuePair("webName","中国电子银行网"));
		nvps.add(new BasicNameValuePair("webSectionName","业内资讯"));
		nvps.add(new BasicNameValuePair("website_id","5890"));
		nvps.add(new BasicNameValuePair("p_url","http://zx.cebnet.com.cn/"));
		nvps.add(new BasicNameValuePair("tagName","银行"));
		nvps.add(new BasicNameValuePair("keyWords","银行，直销"));
		httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		HttpResponse response = httpclient.execute(httpost);
		//System.out.println(response.getStatusLine());
		HttpEntity entity = response.getEntity();
		String s= EntityUtils.toString(entity);
		System.out.println(s);
	}
	
}
